package com.ygl.framework.mvc;

import com.ygl.framework.exception.MyException;
import com.ygl.utils.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author ygl
 * @date 2021/7/7
 * @description mvc层的异常处理
 */
@ControllerAdvice
public class GlobalControllerAdvice {

    /**
     * @author ygl
     * @date 2021/7/7
     * @description 针对所有异常
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Result handle(RuntimeException exception) {
        exception.printStackTrace();
        return Result.fail(exception.getMessage());
    }

    /**
     * @author ygl
     * @date 2021/7/8
     * @description 自定义异常
     */
    @ExceptionHandler(MyException.class)
    @ResponseBody
    public Result handle(MyException exception) {
        exception.printStackTrace();
        return Result.fail(exception.getMessage());
    }
}
